﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTBusiness.DB_Linq;
using TTBusiness;
using System.IO;
using System.Text;
using TTController;
namespace TTMarketing.UserControl.GioHang
{
    public partial class ucGioHang : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("delete"))
                {

                    List<clsBSOrder> order = new List<clsBSOrder>();
                    if (Session["order"] != null)
                    {
                        order = (List<clsBSOrder>)Session["order"];
                    }
                    for (int i = 0; i < order.Count; i++)
                    {
                        if (order[i].ProductId.Equals(Convert.ToInt32(e.CommandArgument)))
                        {
                            order.RemoveAt(i);
                            break;
                        }
                    }
                    Session["order"] = order;
                }
                else
                    if (e.CommandName.Equals("update"))
                    {
                        List<clsBSOrder> order = new List<clsBSOrder>();
                        if (Session["order"] != null)
                        {
                            order = (List<clsBSOrder>)Session["order"];
                        }
                        for (int i = 0; i < order.Count; i++)
                        {
                            if (order[i].ProductId.Equals(Convert.ToInt32(e.CommandArgument)))
                            {
                                Telerik.Web.UI.RadNumericTextBox textbox = new Telerik.Web.UI.RadNumericTextBox();
                                textbox = (Telerik.Web.UI.RadNumericTextBox)this.RadGrid1.MasterTableView.Items[e.Item.ItemIndex]["count"].Controls[1];
                                order[i].Count = Convert.ToInt32(textbox.Value);
                                break;
                            }
                        }
                        Session["order"] = order;
                    }
                this.RadGrid1.Rebind();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                tblOrder objO = new tblOrder();
                TTBusiness.ClassDB.clsBSOrder objOB = new TTBusiness.ClassDB.clsBSOrder();
                TTBusiness.ClassDB.clsBSOrderDetail objODT = new TTBusiness.ClassDB.clsBSOrderDetail();
                TTBusiness.ClassDB.clsBSProduct objPB = new TTBusiness.ClassDB.clsBSProduct();
                clsCTProcess objProcess = new clsCTProcess();
                objO.Address = this.txtAddress.Text;
                objO.Detail = this.txtMessage.Text;
                objO.Email = this.txtEmail.Text;
                objO.Phone = this.txtPhone.Text;
                objO.FullName = this.txtFullName.Text;
                int id = objProcess.Order_insert(objO);
                List<clsBSOrder> order = new List<clsBSOrder>();
                if (Session["order"] != null)
                {
                    order = (List<clsBSOrder>)Session["order"];
                }
                for (int i = 0; i < order.Count; i++)
                {
                    tblProduct objP = new tblProduct();
                    objP=objPB.getById(order[i].ProductId);
                    tblOrderDetail objOD = new tblOrderDetail();
                    objOD.Price = objP.Price;
                    objOD.ProductID = objP.ID;
                    objOD.Count = order[i].Count;
                    objOD.OrderID = id;
                    objODT.insert(objOD);
                }
                Session["order"] = null;
                clsBSSendMail.sendMail(new string[] { "thiepnd@tantinh.net" }, "Thừ của khách hàng gửi từ website", "Hãy đăng nhập vào tài khoản admin để xem chi tiết");
                Response.Redirect("~/cam-on.aspx");
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public string RenderControl(Control ctrl)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctrl.RenderControl(hw);
            return sb.ToString();
        }
    }
}